<template>
	<view>
		<image src="../../static/index.png" style="width: 100%;"></image>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">学号</view>
				<input placeholder="请输入您的学号" v-model="userNumber" type="number"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">密码</view>
				<input placeholder="请输入密码" v-model="password" password="true" type="number"></input>
			</view>
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-blue margin-tb-sm lg" @tap="login">登陆</button>
				<button class="cu-btn bg-grey lg" @tap="showModal" data-target="Image">说明</button>
			</view>
		</form>
		<view class="cu-modal" :class="modalName=='Image'?'show':''">
			<view class="cu-dialog">
				<view class="bg-img" style="background-image: url('https://ossweb-img.qq.com/images/lol/web201310/skin/big91012.jpg');height:200px;">
					<view class="cu-bar justify-end text-white">
						<view class="action" @tap="hideModal">
							<text class="cuIcon-close "></text>
						</view>
					</view>
				</view>
				<view class="cu-bar bg-white">
					<view class="action margin-0 flex-sub  solid-left" @tap="hideModal">我知道了</view>
				</view>
			</view>
		</view>
	</view>


</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				userNumber: '',
				password: '',
				modalName: null,
				endTime: '2019-11-25 00:00:00'
			}
		},
		onLoad() {
			// uni.authorize({
			//     scope: 'scope.userInfo',
			//     success() {
			//         uni.getUserInfo({
			//         	provider: 'weixin',
			//         	success: function(res) {
			//         		console.log(res.userInfo);
			//         	}
			//         });
			//     }
			// })
			uni.request({
				url: 'http://localhost:8080/api/endTime',
				method: 'GET',
				data: {},
				success: res => {
					this.endTime=res.data
				},
				fail: () => {},
				complete: () => {}
			});
		},
		methods: {
			goStudyList() {
				uni.redirectTo({
					url: '../studyList/studyList',
				});
			},
			login() {
				let now = new Date();
				let endTime = new Date(this.endTime);
				if (now.getTime() > endTime.getTime()) {
					uni.showToast({
						title: '选课时间已结束!',
						icon: 'none'
					});
					return;
				}
				if (this.userNumber == '' || this.password == '') {
					uni.showToast({
						title: '用户名或密码不能为空!',
						icon: 'none'
					});
					return;
				}
				uni.request({
					url: 'http://localhost:8080/api/login',
					method: 'POST',
					data: {
						userNumber: this.userNumber,
						userPassword: this.password
					},
					success: res => {
						if (res.data=='00') {
							uni.setStorage({
								key:'userNumber',
								data:this.userNumber
							});
							this.goStudyList();
						} else {
							uni.showToast({
								title: '用户名或密码错误!',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '请求失败!',
							icon: 'none'
						});
					},
					complete: () => {}
				});
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.modalName = null
			},
		},
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
